<?php

/*
Sponsor Manager (class.db.php file)
Copyright (C) <Fri Feb 08 21:37:40 CET 2008>  Marcos Ramírez <marcosramirez@gmail.com>
Based on my ex Co-Worker Stefan Aichholzer DB.class (http://aichholzer.name)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

class DB {
	private static $conID;
	private static $queryID;
	private static $database;

	function __construct($db) {
		global $Cfg;
		$this->database = $db;
		$this->conID = @mysql_connect($Cfg['DB']['host'], $Cfg['DB']['user'], $Cfg['DB']['pass']);
		if(!$this->conID)
		die(_("DB connection error: ") . mysql_error());
		else
		if(!@mysql_select_db($this->database, $this->conID))
		die(_("Table selection error: ") . mysql_error());

	}

	public function Rows($query = false) {
		return (!$query) ? mysql_num_rows($this->queryID) : mysql_num_rows($query);
	}

	public function Field($cnt, $field, $query = false) {
		return (!$query) ? mysql_result($this->queryID, $cnt, $field) : mysql_result($query, $cnt, $field);
	}
	
	public function Error() { 
		return mysql_error(); 
	}

	public function Query($sql) {
		$this->queryID = mysql_query($sql, $this->conID);
		return (!$this->queryID) ? false : $this->queryID;
	}

	public function Fetch($query = false, $method = false) {
		$assoc = (!$method) ? MYSQL_ASSOC : $method;
		return (!$query) ? mysql_fetch_array($this->queryID, $assoc) : mysql_fetch_array($query, $assoc);
	}
	
	public function FetchAll () {
		while ($row =$this->fetch($this->queryID)){ $all[] = $row; }
		return $all;
	}

	public function lastid () {
		return mysql_insert_id($this->conID);
	}

	public function existsOnTable ($table, $column, $register) {
		$res=$this->Query("SELECT $column FROM $table WHERE $column='$register'");
		if (is_array($resultado=$this->Fetch()))
		return TRUE;
		else
		return FALSE;
	}
}

?>